package com.hyphenate.easeui.widget.chatrow;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMMessage.ChatType;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.easeui.R;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.hyphenate.exceptions.HyphenateException;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.net.http.RequestQueue;
import android.text.Spannable;
import android.text.TextUtils;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.BufferType;

import org.apache.commons.lang3.StringUtils;

public class EaseChatRowText extends EaseChatRow{

    private TextView contentView,ImageTitle;
    private LinearLayout imageLL;
    private ImageView Image;
    String artUrl = null;//收藏品图片地址
    String artDescribe=null;//收藏品介绍3

    public EaseChatRowText(Context context, EMMessage message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);
    }

    @Override
    protected void onInflateView() {
        inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
                R.layout.ease_row_received_message : R.layout.ease_row_sent_message, this);
    }

    @Override
    protected void onFindViewById() {
        contentView = (TextView) findViewById(R.id.tv_chatcontent);
    }

    @Override
    public void onSetUpView() {
        imageLL=(LinearLayout) findViewById(R.id.ease_row_received_message_ImageLL);
        Image=(ImageView) findViewById(R.id.ease_row_received_message_Image);
        ImageTitle=(TextView) findViewById(R.id.ease_row_received_message_ImageTitle);
        try {
            artUrl=message.getStringAttribute("artUrl");
            artDescribe=message.getStringAttribute("artDescribe");
            ImageLoader.getInstance().displayImage(artUrl, Image);
            if (!TextUtils.isEmpty(artDescribe)) {
                ImageTitle.setText(artDescribe);
            }
            imageLL.setVisibility(View.VISIBLE);
            contentView.setVisibility(View.GONE);
        } catch (HyphenateException e) {
            // TODO Auto-generated catch block
            artUrl="";
            artDescribe="";
        }
        if(StringUtils.isEmpty(artUrl)){
            contentView.setVisibility(View.VISIBLE);
            imageLL.setVisibility(View.GONE);
            EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
            Spannable span;
            if(!StringUtils.isEmpty(StringUtils.substringAfterLast(txtBody.getMessage(), "#"))){
                span = EaseSmileUtils.getSmiledText(context, StringUtils.substringAfterLast(txtBody.getMessage(), "#"));
            }else{
                span = EaseSmileUtils.getSmiledText(context, txtBody.getMessage());
            }
            // 设置内容
            contentView.setText(span, BufferType.SPANNABLE);
        }
        handleTextMessage();
    }

    protected void handleTextMessage() {
        if (message.direct() == EMMessage.Direct.SEND) {
            setMessageSendCallback();
            switch (message.status()) {
                case CREATE:
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    break;
                case SUCCESS:
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.GONE);
                    break;
                case FAIL:
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    break;
                case INPROGRESS:
                    progressBar.setVisibility(View.VISIBLE);
                    statusView.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        }else{
            if(!message.isAcked() && message.getChatType() == ChatType.Chat){
                try {
                    EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onBubbleClick() {
        // TODO Auto-generated method stub

    }



}
